package com.yang.service.Impl;


import com.yang.entity.Address;
import com.yang.entity.OrderItem;
import com.yang.mapper.AddressMapper;
import com.yang.mapper.OrderMapper;
import com.yang.mapper.UserMapper;
import com.yang.entity.User;
import com.yang.service.UserService;
import com.yang.utils.Md5Util;
import com.yang.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;

/**
 * @author 杨鑫
 * 用户服务层实现类
 * */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private AddressMapper addressMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public User login(String userAccount, String userPassword) {
//        返回的用户对象
        return userMapper.userLogin(userAccount, userPassword);
    }

    @Override
    public void register(String userAccount, String userPassword) {
        // 加密
        String md5String = Md5Util.getMD5String(userPassword);
        // 调用dao层方法插入用户信息
        userMapper.insertUser(userAccount, md5String);
    }

    @Override
    public User findByUserAccount(String userAccount) {
        // 根据账号查询用户信息
        return userMapper.findByUserAccount(userAccount);

    }



    @Override
    public void updatePassword(String userAccount, String userPassWord) {
        // 加密
        String newPassWord = Md5Util.getMD5String(userPassWord);
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("userId");
        System.out.println(userId);
        // 调用mapper更新密码
        userMapper.updatePassWord(userId, newPassWord);
    }

    @Override
    public void updateAvatar(Integer userId, String url) {
        // 调用mapper更新头像
        userMapper.updateAvatar(userId, url);
    }


}
